<?php
namespace app\index\controller;
use think\Controller;
use app\common\controller\HomeBase;
use think\Request;
// 引入AipSpeech.php
use baidu\AipSpeech;

class Soul extends HomeBase
{
    protected $app_id;
    protected $app_key;
    protected $secret_key;
    protected $client;
    protected function _initialize()
    {
        parent::_initialize();
        /*
         * 百度voice识别appkey
         * */
        $this->app_id = '9921025';
        $this->app_key = 'DeZUbKuxI8SNSqZn6HTxaTIp';
        $this->secret_key = 'ltX5evgvVN2M7dpOZU3wP4BlY7XWL7TU';
        $this->client = new AipSpeech($this->app_id, $this->app_key, $this->secret_key);
    }
    public function baidu_amr($name){
        $head_path = './uploads/';
        //  str_replace('\\', '/', $save_path . date('Ymd') . DS . $name);
        $amr_path = str_replace('\\', '/', $head_path . date('Ymd') . DS . $name);
//        file_put_contents('./requestMsg.log','--MC--soul方法解码结果 :'.$amr_path."\n",FILE_APPEND); // $media_id
        $result = $this->client->asr(file_get_contents($amr_path), 'amr', 8000, array('dev_pid' => 1536,));
//        file_put_contents('./requestMsg.log','--MC--soul方法解码结果 :'.$result."\n",FILE_APPEND); // $media_id
        return $result;
    }



    public function amr1005(){
        // https://ae.shangning.org.cn/uploads/20181005/d667251248ce33f837ff99ed51179fb9.amr
        $result = $this->client->asr(file_get_contents('./uploads/20181005/88cdb548c6b0158716ff43d98c729a0e.amr'), 'amr', 8000, array('dev_pid' => 1536,));
        return json_encode($result,JSON_UNESCAPED_UNICODE);
    }

    public function testpcm(){
        $result = $this->client->asr(file_get_contents('./test.pcm'), 'pcm', 16000, array('dev_pid' => 1536,));
        return json_encode($result,JSON_UNESCAPED_UNICODE);
    }

    public function testam(){
        $result = $this->client->asr(file_get_contents('./1673c2c6a6280e720ba87207cf6cf550.amr'), 'amr', 8000, array('dev_pid' => 1536,));
        return json_encode($result,JSON_UNESCAPED_UNICODE);
    }

    public function testam2(){
        $result = $this->client->asr(file_get_contents('./uploads/20180929/1673c2c6a6280e720ba87207cf6cf550.amr'), 'amr', 8000, array('dev_pid' => 1536,));
        return json_encode($result,JSON_UNESCAPED_UNICODE);
    }

    public function testamr(){
        // /uploads/20180929/ade6e81eacea93722139231084da134b.amr  echo $_SERVER['HTTP_HOST']."<br>"; #localhost
        $amr_url =  '/uploads/20180929/1673c2c6a6280e720ba87207cf6cf550.amr';
        $result = $this->client->asr('https://ae.shangning.org.cn/uploads/20180929/1673c2c6a6280e720ba87207cf6cf550.amr', 'amr', 8000, array('dev_pid' => 1536,));
        return json_encode($result,JSON_UNESCAPED_UNICODE);

    }

    public function baidu_amr2($data){
        $data = '.'.$data;
        file_put_contents('./requestMsg.log','--MC--soul方法解码 :'.$data."\n",FILE_APPEND); // $media_id
        $result = $this->client->asr(file_get_contents($data), 'amr', 8000, array('dev_pid' => 1536,));
        file_put_contents('./requestMsg.log','--MC--soul方法解码结果 :'.$result."\n",FILE_APPEND); // $media_id
//        return json_encode($result,JSON_UNESCAPED_UNICODE);
        return $result;
    }









}


